package cn.sfrog.project.biz.applet.controller;

import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.redis.RedisCache;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.framework.web.service.TokenService;
import cn.sfrog.project.biz.applet.entity.AppCompanyEntity;
import cn.sfrog.project.biz.applet.entity.VO.*;
import cn.sfrog.project.biz.applet.service.AppExtOderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.util.Date;
import java.util.List;


/**
 * @Author chenl
 * @Date 2023-12-01 14:29
 * @title AppExtOderController
 * @description
 **/
@Slf4j
@RestController
@RequestMapping("/api/externalOrder")
@Api(value = "/api/externalOrder", tags = "外部订单相关处理")
public class AppExtOderController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    private AppExtOderService appExtOderService;
    @ApiOperation(value = "外部订单录入")
    @PostMapping("/receiveExternalOrder")
    public ApiAjaxResult receiveExternalOrder(@RequestBody AppcompanyVo appcompanyVo,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token.equals("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImZkZmVlMGI4LTExZWMtNDJkZi04MmJmLTE3ZDkzYjMwOGIyMyJ9.2sOvGZOQ9JGX8myEHadnNK1rXjMaIAwUOUg30cD-4n6nCHnb7ulY5tcen6ejRYySzyhIVSPwIKw_LIKC0b4ddA")){
            appcompanyVo.setCreateTime(new Date());
            return appExtOderService.receiveExternalOrder(appcompanyVo);
        }else {
            return ApiAjaxResult.error("用户验证失败，检查token是否正确",new Date());
        }

    }
    @ApiOperation(value = "外部订单修改订单状态")
    @PostMapping("/changeExternalOrderStatus")
    public ApiAjaxResult changeExternalOrderStatus(@RequestBody AppCompanyStatusVo appCompanyStatusVo,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token.equals("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImZkZmVlMGI4LTExZWMtNDJkZi04MmJmLTE3ZDkzYjMwOGIyMyJ9.2sOvGZOQ9JGX8myEHadnNK1rXjMaIAwUOUg30cD-4n6nCHnb7ulY5tcen6ejRYySzyhIVSPwIKw_LIKC0b4ddA")){
            return appExtOderService.changeExternalOrderStatus(appCompanyStatusVo);
        }else {
            return ApiAjaxResult.error("用户验证失败，检查token是否正确",new Date());
        }
//        return appExtOderService.changeExternalOrderStatus(appCompanyStatusVo);
    }
    @ApiOperation(value = "订单接单状态-陪诊信息")
    @PostMapping("/getExternalAcceptOrderStatus")
    public ApiAjaxResult getExternalAcceptOrderStatus(String serviceOrderSn){
        return appExtOderService.getExternalAcceptOrderStatus(serviceOrderSn);
    }
    @ApiOperation(value = "删除外部订单")
    @PostMapping("/deletExternalOrder")
    public ApiAjaxResult delExternalOrder(String serviceOrderSn){
        return appExtOderService.delExternalOrder(serviceOrderSn);
    }

    @ApiOperation(value = "获取订单列表")
    @GetMapping("/externalOrderList")
    public ApiAjaxResult externalOrderList(){
        List<AppCompanyEntity> appCompanyEntities = appExtOderService.externalOrderList();
        return ApiAjaxResult.success(appCompanyEntities);
    }
    @ApiOperation(value = "评价订单")
    @PostMapping("/updataExternalOrder")
    public ApiAjaxResult updataExternalOrder(@RequestBody EvaluationVo evaluationVo){
        AppCompanyEntity appCompanyEntity = new AppCompanyEntity();
        if (StringUtils.isNotEmpty(evaluationVo.getEvaluation())){
            // 已评价 设置订单
            appCompanyEntity.setOrderStatus("5");
            appCompanyEntity.setEvaluationStatus(1);
        }
        if (StringUtils.isNotEmpty(evaluationVo.getComplaint())){
            appCompanyEntity.setComplaintStatus(1);
        }
        appExtOderService.updataExternalOrder(appCompanyEntity);
        return ApiAjaxResult.success();
    }
    @ApiOperation(value = "陪诊结束")
    @PostMapping("/confirmAccompanying")
    //  修改订单状态为3 订单号
    public ApiAjaxResult confirmAccompanying(ConfirmAccompanyVO confirmAccompanyVO){
        //  确定陪诊
        appExtOderService.confirmAccompanying(confirmAccompanyVO);
        return ApiAjaxResult.success();
    }

    @ApiOperation(value = "推送陪诊人员到山东",httpMethod = "POST")
    @PostMapping(value = "/shandongOrderPushAccompany")
    public ApiAjaxResult shandongOrderPushAccompany(@RequestBody ShandongOrderPushAccompanyVo shandongOrderPushAccompanyVo){
        return appExtOderService.shandongOrderPushAccompany(shandongOrderPushAccompanyVo);
    }

}
